package com.taritari.service;

import com.alibaba.fastjson.JSON;
import com.taritari.entity.Content;
import com.taritari.utils.HtmlParseUtil;
import com.taritari.vo.ContentVo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ContentService
 * @Description TODO
 * @date 2021/9/30 15:29
 * @Version 1.0
 */
@Service
public class ContentService {
    @Autowired
    private RestHighLevelClient  restHighLevelClient;

    //解析数据，放入Es中
    public Boolean parseContent(String keywords) throws IOException {
        ArrayList<Content> contents = new HtmlParseUtil().parseJD(keywords);
        //插入es索引库中
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");

        for (int i = 0; i < contents.size(); i++) {
            bulkRequest.add(new IndexRequest("jd_goods")
            .source(JSON.toJSONString(contents.get(i)),XContentType.JSON));
        }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulk.hasFailures();
    }
    //搜索
    public List<Map<String,Object>> search(String keyword,int pageNo,
                                           int pageSize) throws IOException {
        if (pageNo<=1){
            pageNo=1;
        }
        SearchRequest request = new SearchRequest("jd_goods");
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        //分页
        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);


        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title",keyword);
        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        request.source(searchSourceBuilder);
        SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        List<Map<String ,Object>> list=new ArrayList<>();


        for (SearchHit hit : search.getHits().getHits()) {
            list.add(hit.getSourceAsMap());
        }
        return list;
    }
    //多条件查询
    public List<Map<String, Object>> getGoods(ContentVo vo) throws IOException {

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        SearchRequest request = new SearchRequest("jd_goods");
        List<Map<String,Object>> billList = new ArrayList<>();
//        Map<String,Object> resultMap = new HashMap<>();
        if (!StringUtils.isEmpty(vo.getTitle()))boolQueryBuilder.must(QueryBuilders.termQuery("title",vo.getTitle()));
        if (!StringUtils.isEmpty(vo.getImg()))boolQueryBuilder.must(QueryBuilders.termQuery("img",vo.getImg()));
        if (!StringUtils.isEmpty(vo.getPrice()))boolQueryBuilder.must(QueryBuilders.termQuery("price",vo.getPrice()));
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from(vo.getPageNo());
        searchSourceBuilder.size(vo.getPageSize());
        searchSourceBuilder.query(boolQueryBuilder);
        request.source(searchSourceBuilder);
        SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : search.getHits().getHits()) {
            billList.add(hit.getSourceAsMap());
        }
        return billList;
    }
}
